#include "Bullet.h"


Bullet::Bullet(GameState *gameState) : PhysicalObject(gameState)
{
	addProperty("bullet");

	getBodyDef()->type = b2_dynamicBody;
	getBodyDef()->bullet = true;
	getBodyDef()->fixedRotation = false;
	getBodyDef()->gravityScale = 0.0f;
	b2FixtureDef *fixDef = new b2FixtureDef;
	fixDef->filter.groupIndex = -2;
	b2PolygonShape *shape = new b2PolygonShape;
	shape->SetAsBox(0.5,0.5);
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->restitution = 0.0f;
	fixDef->isSensor = true;
	addFixtureDef(fixDef);
	setRotation(0.0f);
}


Bullet::~Bullet()
{
}

void Bullet::firstUpdate(){
	b2Vec2 f = m_body->GetWorldVector(b2Vec2(0,100));
    
	b2Vec2 p = m_body->GetWorldCenter();
	m_body->ApplyLinearImpulse(f,p); 
	m_firstUpdate = false;
}

void Bullet::update(double dt){
	
}

void Bullet::beginContact(b2Contact *contact){
	PhysicalObject *me = (PhysicalObject*)contact->GetFixtureA()->GetBody()->GetUserData();
	PhysicalObject *other = (PhysicalObject*)contact->GetFixtureB()->GetBody()->GetUserData();

	if(me != this){ 
		PhysicalObject *temp;
		temp = me;
		me = other;
		other = temp;
	}

	this->kill();
}
